package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.EventSignUpService;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class RegisterToEventServlet
 */
@WebServlet("/RegisterToEventServlet")
public class RegisterToEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterToEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	/**
	 * Method invoked when the visitor wants to register for an event
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		
		int idVisitor = 0;
		int idEvent1 = 0;
        
		HttpSession session = request.getSession(true);
		
		EventSignUpService service = new EventSignUpService();
		EventSignUp eventsu = new EventSignUp();
		EventService eservice = new EventService();
		Event ev = new Event();
		
		String n=(String) session.getAttribute("username");
		
        VisitorService vservice = new VisitorService();
		
		Visitor v = new Visitor();
		v.setUserName(n);
		
		idVisitor = vservice.getIdVisitor(v);
		/**
		 * Get the id of event
		 */
		idEvent1 = Integer.parseInt(request.getParameter("idEventAll"));
	
		
		eventsu.setIdVisitor(idVisitor);
		eventsu.setIdEvent(idEvent1);
		
		/**
		 * If the visitor is not registered at the event
		 */
	    if(!service.checkEventsofVisitor(eventsu)){	
	    	
	    	/**
	    	 * The visitor is recorded in the event
	    	 */
	    	service.registerVisitorToEvent(eventsu);
	    	
	    	ev.setEventID(idEvent1);
	    	/**
	    	 * Available seats decrement event
	    	 */
	    	eservice.updateSeatsEventDec(ev);
	    	
	    	/**
			 * Invokes the action "PersonalServlet"
			 */
	    	response.sendRedirect("PersonalServlet");
	    
	    	/**
	    	 * If the visitor is registered in the event
	    	 */
	    } else {
	    	
	    	RequestDispatcher rd = request.getRequestDispatcher("AlreadyRegistered.jsp");
			rd.forward(request, response);
	    	
	    }
	    		
		
		
	}

}
